local class = require 'hump.class'
local state = require 'utilities.state'
local tableutils = require 'utilities.tableutils'

local filenames = {}
local function fillFilenames()
  for i=1, 6 do
    filenames[i] = 'images/planet'..i..'.png'
  end
  tableutils.shuffle(filenames)
end

Planet = class{
  function (self, filename, cx, cy, radius, weight, collider, spacephysics)
    if not filename then
      if #filenames == 0 then
        fillFilenames()
      end
      filename = filenames[1]
      table.remove(filenames, 1)
    end
    self.filename = filename
    self.physics = spacephysics
    self.planetphysics = self.physics:newItem(weight, cx, cy)
    self.planetphysics:setMaximumSpeed(0)
    self.collider = collider
    self.planetcollider = self.collider:addCircle(cx, cy, radius)
  end
}

function Planet:state()
  local cx, cy = self.planetcollider:center()
  local px1,py1,px2,py2 = self.planetcollider:bbox()
  local r = px2-px1
  return state.new(cx, cy, r, r, 0, self.filename)
end
